package com.countantProject.ui;

import com.countantProject.bean.Customer;
import com.countantProject.service.CustomerList;
import com.countantProject.util.CMUtility;

/**
 * @author Lenl
 * @create 2021-05-29 11:49
 */
public class CustomerView {
    private CustomerList customerList=new CustomerList(10);

    public CustomerView(){
        Customer customer=new Customer("张三",'男',23,"12332114562","zs123@163.com");
        customerList.addCustomer(customer);
    }

    /**
     * 显示主界面
     */
    public void enterMainMenu(){
        boolean isFlag=true;
        while(isFlag){
            System.out.println("\n----------------------客户信息管理软件----------------------");
            System.out.println("                        1.添 加 客 户                        ");
            System.out.println("                        2.修 改 客 户                        ");
            System.out.println("                        3.删 除 客 户                        ");
            System.out.println("                        4.客 户 列 表                        ");
            System.out.println("                        5.  退   出                        \n");
            System.out.print("                   请选择(1-5):");

            char menu= CMUtility.readMenuSelection();
            switch(menu){
                case '1':
                    addNewCustomer();
                    break;
                case '2':
                    modifyCustomer();
                    break;
                case '3':
                    deleteCustomer();
                    break;
                case '4':
                    listAllCustomers();
                    break;
                case '5':
                    System.out.print("确认是否退出(y/n):");
                    char isExit= CMUtility.readConfirmSelection();
                    if(isExit=='Y'){
                        isFlag=false;
                        System.out.print("...\n成功退出系统！");
                    }
            }
        }

    }

    /**
     * 添加客户操作
     */
    private void addNewCustomer(){
        System.out.println("--------------------------添 加 客 户--------------------------");
        System.out.print("姓名：");
        String name= CMUtility.readString(10);
        System.out.print("性别：");
        char gender=CMUtility.readChar();
        System.out.print("年龄：");
        int age=CMUtility.readInt();
        System.out.print("电话：");
        String phone=CMUtility.readString(13);
        System.out.print("邮箱：");
        String email=CMUtility.readString(30);
        //将上述数据封装到对象中
        Customer customer=new Customer(name,gender,age,phone,email);
        boolean isSuccess=customerList.addCustomer(customer);
        if(isSuccess){
            System.out.println("--------------------------添 加 成 功！--------------------------");

        }
        else{
            System.out.println("-------------------添加失败！客户列表已满。-------------------");
        }

    }
    /**
     * 修改客户操作
     */
    private void modifyCustomer(){
        System.out.println("--------------------------修 改 客 户--------------------------");
        Customer cust=new Customer();
        int number;
        for (;;){
            System.out.print("请选择待修改客户编号(-1退出)：");
            number=CMUtility.readInt();
            if(number==-1){
                return;
            }
            cust=customerList.getCustomer(number-1);
            if(cust==null){
                System.out.println("未找到指定客户！");
            }else{//找到了相应编号的客户
                break;
            }
        }
        System.out.print("姓名("+cust.getName()+")：");
        String name=CMUtility.readString(10,cust.getName());
        System.out.print("性别("+cust.getGender()+")：");
        char gender=CMUtility.readChar(cust.getGender());
        System.out.print("年龄("+cust.getAge()+")：");
        int age=CMUtility.readInt(cust.getAge());
        System.out.print("电话("+cust.getPhone()+")：");
        String phone=CMUtility.readString(13,cust.getPhone());
        System.out.print("邮箱("+cust.getEmail()+")：");
        String email=CMUtility.readString(30,cust.getEmail());

        Customer newCust=new Customer(name,gender,age,phone,email);
        boolean isReplaced= customerList.replaceCustomer(number-1,newCust);
        if(isReplaced){
            System.out.println("--------------------------修 改 成 功！--------------------------");
        }else{
            System.out.println("--------------------------修 改 失 败！--------------------------");
        }

    }
    /**
     * 删除客户操作
     */
    private void deleteCustomer(){
        System.out.println("--------------------------删 除 客 户--------------------------");
        int number;
        for (;;){
            System.out.print("请选择待删除客户编号(-1退出)");
            number=CMUtility.readInt();
            if(number==-1){
                return;
            }
            Customer cust=customerList.getCustomer(number-1);
            if(cust==null){
                System.out.println("未找到指定客户");
            }else{
                break;
            }
        }
        //找到了客户
        System.out.print("确认是否删除(y/n):");
        char isDelete=CMUtility.readConfirmSelection();
        if (isDelete=='Y'){
            boolean deleteSuccess= customerList.deleteCustomer(number-1);
            if(deleteSuccess){
                System.out.println("--------------------------删 除 成 功！--------------------------");
            }
            else{
                System.out.println("--------------------------删 除 失 败！--------------------------");

            }
        }else{
            return;
        }
    }
    /**
     * 展示客户列表操作
     */
    private void listAllCustomers(){
        System.out.println("\n--------------------------客 户 列 表--------------------------");
        int total=customerList.getTotal();
        if(total==0){
            System.out.println("没有客户记录！");
        }else{
            System.out.println("编号\t\t姓名\t\t性别\t\t年龄\t\t\t电话\t\t\t\t邮箱");
            Customer[] custs= customerList.getAllCustomers();
            for (int i=0;i<custs.length;i++){
                Customer cu=custs[i];
                System.out.println((i+1)+"\t\t"+cu.getName()+"\t\t"+cu.getGender()+"\t\t"+
                        cu.getAge()+"\t\t"+cu.getPhone()+"\t\t"+cu.getEmail());
            }
        }
    }

    public static void main(String[] args) {
        CustomerView view=new CustomerView();

        view.enterMainMenu();
    }

}
